<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Agp\CoreBundle\Manager;

use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Inject;
use Symfony\Component\DependencyInjection\Container;
use Agp\CoreBundle\Entity\Layout;
use Agp\CoreBundle\Entity\LayoutTicket;
        
/**
 * Description of LayoutManager
 *
 * @author bondcs
 * @service("agp.layout.manager")
 */
class LayoutManager {
    
    protected $container;

    /**
     * @InjectParams({
     *     "container" = @Inject("service_container")
     * })
     */
    public function __construct(Container $container) {
        $this->container = $container;
    }
    
    public function persist($data){
        
        $layout = new Layout;
        $this->geraComponentes($data, $layout);
    
    }
    
    public function update($data, $layout){
        
        $this->container->get('agp.layoutTicket.repository')->deleteComponentes($layout);
        $this->geraComponentes($data, $layout);
    
    }
    
    public function geraComponentes($data, $layout){
        
        $componentes = $this->container->get('agp.componente.repository')->findAll();
        //var_dump($componentes);
        $user = $this->container->get('security.context')->getToken()->getUser();
        $utils = $this->container->get('admin.utils');
        $em = $this->container->get('doctrine')->getEntityManager();
        
        // Acrecenta +1 para as posições
        $posicoesComBaseZero = $this->container->get('session')->get('posicoes'); 
        foreach ($posicoesComBaseZero as $key => $posicao){
            $posicoes[$key+1] = $posicao;
        }
        
        $layout->setDescricao($data['descricaoLayout']);
        $layout->setEmpresa($user->getEmpresa());
        
        $em->persist($layout);
        $em->flush();
        
        $layoutTicket = new LayoutTicket;
        $layoutTicket->setComponente($componentes[0]);
        $layoutTicket->setLayout($layout);
        $layoutTicket->setAtributo($data['logo']);
        $em->persist($layoutTicket);
        
        $titulo = '';
        if ($data['titulo1'] != ''){
            $titulo .= $utils->replace_accents($data['titulo1']).'*';
        }
        
        if ($data['titulo2'] != ''){
            $titulo .= $utils->replace_accents($data['titulo2']).'*';
        } 
        
        if ($data['titulo3'] != ''){
            $titulo .= $utils->replace_accents($data['titulo3']).'*';
        }
        
        if ($data['titulo4'] != ''){
            $titulo .= $utils->replace_accents($data['titulo4']).'*';
        }
        
        $layoutTicket01 = new LayoutTicket;
        $layoutTicket01->setComponente($componentes[1]);
        $layoutTicket01->setLayout($layout);
        $layoutTicket01->setAtributo($titulo);
        $layoutTicket01->setIndice(array_search('titulo', $posicoes) ? array_search('titulo', $posicoes) : -1);
        $em->persist($layoutTicket01);
        
        $layoutTicket02 = new LayoutTicket;
        $layoutTicket02->setComponente($componentes[2]);
        $layoutTicket02->setLayout($layout);
        $layoutTicket02->setAtributo($data['categoria']);
        $layoutTicket02->setIndice(array_search('categoria', $posicoes) ? array_search('categoria', $posicoes) : -1);
        $em->persist($layoutTicket02);
        
        $layoutTicket03 = new LayoutTicket;
        $layoutTicket03->setComponente($componentes[3]);
        $layoutTicket03->setLayout($layout);
        $layoutTicket03->setAtributo($data['produto']);
        $layoutTicket03->setIndice(array_search('produto', $posicoes) ? array_search('produto', $posicoes) : -1);
        $em->persist($layoutTicket03);
        
        $layoutTicket04 = new LayoutTicket;
        $layoutTicket04->setComponente($componentes[4]);
        $layoutTicket04->setLayout($layout);
        $layoutTicket04->setAtributo($data['valor']);
        $layoutTicket04->setIndice(array_search('valor', $posicoes) ? array_search('valor', $posicoes) : -1);
        $em->persist($layoutTicket04);
        
        $layoutTicket05 = new LayoutTicket;
        $layoutTicket05->setComponente($componentes[5]);
        $layoutTicket05->setLayout($layout);
        $layoutTicket05->setAtributo($data['dataImpressao']);
        $layoutTicket05->setIndice(array_search('dataImpressao', $posicoes) ? array_search('dataImpressao', $posicoes) : -1);
        $em->persist($layoutTicket05);
        
        $descricao = '';
        if ($data['descricao1'] != ''){
            $descricao .= $utils->replace_accents($data['descricao1']).'*';
        }
        
        if ($data['descricao2'] != ''){
            $descricao .= $utils->replace_accents($data['descricao2']).'*';
        } 
        
        if ($data['descricao3'] != ''){
            $descricao .= $utils->replace_accents($data['descricao3']).'*';
        }
        
        if ($data['descricao4'] != ''){
            $descricao .= $utils->replace_accents($data['descricao4']).'*';
        }
        
        $layoutTicket06 = new LayoutTicket;
        $layoutTicket06->setComponente($componentes[6]);
        $layoutTicket06->setLayout($layout);
        $layoutTicket06->setAtributo($descricao);
        $layoutTicket06->setIndice(array_search('descricao', $posicoes) ? array_search('descricao', $posicoes) : -1);
        $em->persist($layoutTicket06);
        
        $layoutTicket07 = new LayoutTicket;
        $layoutTicket07->setComponente($componentes[7]);
        $layoutTicket07->setLayout($layout);
        $layoutTicket07->setAtributo(null);
        $layoutTicket07->setIndice(array_search('codigoBarra', $posicoes) ? array_search('codigoBarra', $posicoes) : -1);
        $em->persist($layoutTicket07);
        
        $em->flush();
        
    }
}

?>
